<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Title</title>
    <style>
        table {
            border-collapse: collapse;
            /* text-align: center; */
        }

        thead {
            background-color: #f5f5f5;
        }

        th, td {
            border: 1px solid #aaa;
            padding: 8px 16px;
        }

        .active {
            background-color: skyblue;
        }

        [v-cloak]{
            display: none;
        }
    </style>
</head>
<body>
<div id="app" v-cloak>
    <template v-if="books.length">
        <table>
            <thead>
            <tr>
                <th>序号</th>
                <th>书籍名称</th>
                <th>出版日期</th>
                <th>价格</th>
                <th>购买数量</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
                <tr v-for="(item,index) in books"
                :key="item.id"
                v-on:click="rowClick(index)"
                :class="{active:index===currentIndex}">
                    <td>{{index+1}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.date}}</td>
                    <td>{{formatPrice(item.price)}}</td>
                    <td>
                        <button :disabled="item.count<=1"
                        v-on:click="decrement(index,item)">
                            -
                        </button>
                        {{item.count}}
                        <button @click="increment(index,item)">
                            +
                        </button>
                    </td>
                    <td>
                        <button v-on:click="removeBook(index,item)">移除</button>
                    </td>
                </tr>
            </tbody>
        </table>
        <h2>总价：{{formatPrice(totalPrice)}}</h2>
    </template>
    <template v-else>
        <h1>暂无书籍</h1>
    </template>
</div>
<script src="./data/data.js"></script>
<script src="https://unpkg.com/vue@3"></script>
<script>
    const app=Vue.createApp({
        data(){
            return{
                books:books,
                currentIndex:0
            }
        },
        computed:{
            totalPrice(){
                return this.books.reduce((preVal,item)=>{
                    return preVal+item.price*item.count
                },0)
            }
        },
        methods:{
            formatPrice(price){
                return '￥'+price
            },
            decrement(index,item){
                item.count--
            },
            increment(index,item){
                item.count++
            },
            removeBook(index,item){
                this.books.splice(index,1)
            },
            rowClick(index){
                this.currentIndex=index
            }
        }
    })
    app.mount("#app")
</script>
</body>
</html>